// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "ark-ext" is now active!');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with  registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = vscode.commands.registerCommand('extension.getCrashAddr', function () {
        // The code you place here will be executed every time your command is executed

        let editor = vscode.window.activeTextEditor;
        if (!editor) {
            vscode.window.showInformationMessage('Not found an active text editor !');
            return;
        }

        if (!editor.selection) {
            vscode.window.showInformationMessage("You're not select any text!");
            return;
        }
        
        let range = new vscode.Range(editor.selection.start, editor.selection.end);
        let text = editor.document.getText(range);
        if (!text || text.length == 0) {
            vscode.window.showInformationMessage("You're not select any text!");
        }
        
        let re = /\b[0-9a-f]{8}\b/ig;
        let addr = null;
        let addrString = "";
        while (addr = re.exec(text)) {
            addrString += addr[0] + " "
        }
        
        editor.edit((editBuilder) => {
            editBuilder.replace(editor.selection, addrString);
        }, { undoStopBefore: false, undoStopAfter: false });
        
        
        vscode.window.showInformationMessage(text);
    });

    context.subscriptions.push(disposable);
}
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;